package com.yanqu.road.server.manger.season;

import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.entity.season.SeasonRewardData;
import com.yanqu.road.logic.bussiness.activity.WasheBusiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.season.SeasonRewardModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SeasonRewardMgr extends TempMgr {

    // 当前第几赛季        key：系统ID
    private static final Map<Integer, Integer> seasonIdMap = new ConcurrentHashMap<>();

    // 赛季奖励配置        key：系统ID，赛季ID，奖励类型，奖励ID
    private static final Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> seasonRewardConfigMap = new ConcurrentHashMap<>();

    /**
     * 获取某种类型的全部配置
     */
    public static Map<Integer, SeasonRewardConfig> getSeasonRewardConfigMap(int systemId, int seasonId, int type) {
        Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> map = seasonRewardConfigMap.get(systemId);
        if (map == null) {
            return new ConcurrentHashMap<>();
        }

        Map<Integer, Map<Integer, SeasonRewardConfig>> map1 = map.get(seasonId);
        if (map1 == null) {
            return new ConcurrentHashMap<>();
        }

        Map<Integer, SeasonRewardConfig> map2 = map1.get(type);
        if (map2 == null) {
            return new ConcurrentHashMap<>();
        }

        return map2;
    }

    /**
     * 根据数据寻找配置
     */
    public static SeasonRewardConfig getSeasonRewardConfig(SeasonRewardData data) {
        Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> map = seasonRewardConfigMap.get(data.getSystemId());
        if (map == null) {
            return null;
        }

        Map<Integer, Map<Integer, SeasonRewardConfig>> map1 = map.get(data.getSeasonId());
        if (map1 == null) {
            return null;
        }

        Map<Integer, SeasonRewardConfig> map2 = map1.get(data.getType());
        if (map2 == null) {
            return null;
        }

        return map2.get(data.getRewardId());
    }

    /**
     * 重新加载配置
     */
    public static void reloadSeasonRewardConfigMap(int systemId, int activityId, int seasonId) {
        Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> map = WasheBusiness.getSeasonRewardConfigMap(ChannelConfig.CHANNEL_CENTER_DB_PARAM, activityId, seasonId);
        seasonRewardConfigMap.put(systemId, map);
        seasonIdMap.put(systemId, seasonId);
    }

    /**
     * 无活动清空配置
     */
    public static void clearSeasonRewardConfigMap(int systemId) {
        seasonIdMap.remove(systemId);
        seasonRewardConfigMap.remove(systemId);
    }

    /**
     * 获取正在使用的配置
     */
    public static Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> getInTimeConfigMap() {
        return seasonRewardConfigMap;
    }

    /**
     * 重新加载玩家数据
     */
    public static void reloadUserData() {
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            gamePlayer.getModule(SeasonRewardModule.class).reloadConfigAndData();
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
